package org.tp8.sampleapp.service.impl;

import javax.jws.WebService;

import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.tp8.sampleapp.exc.SampleAppException;
import org.tp8.sampleapp.model.Student;
import org.tp8.sampleapp.persistence.repository.StudentRepository;
import org.tp8.sampleapp.rule.intf.RuleService;
import org.tp8.sampleapp.rule.model.RuleModelWrapper;
import org.tp8.sampleapp.service.abs.AbstractEntityService;
import org.tp8.sampleapp.service.intf.StudentService;
import org.tp8.sampleapp.util.SampleAppLogger;

/**
 * The Class StudentServiceImpl.
 */
@Transactional(propagation = Propagation.REQUIRED)
@Service(value = "studentService")
@WebService(endpointInterface = "org.tp8.sampleapp.service.intf.StudentService")
public class StudentServiceImpl extends
		AbstractEntityService<Long, Student, StudentRepository> implements
		StudentService {

	/**
	 * The Constant log.
	 */
	public final static SampleAppLogger log = new SampleAppLogger(
			StudentServiceImpl.class);

	/**
	 * The student repository.
	 */
	@Autowired
	private StudentRepository studentRepository;

	/** The rule service. */
	@Autowired
	private RuleService ruleService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.tp8.sampleapp.service.abs.AbstractEntityService#getRepository()
	 */
	@Override
	public StudentRepository getRepository() {
		return studentRepository;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.tp8.sampleapp.service.BaseEntityService#save(org.tp8.sampleapp.model
	 * .AbstractModel)
	 */
	@Override
	public Student save(Student t) throws SampleAppException {
		return getRepository().save(t);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.tp8.sampleapp.service.BaseEntityService#findOne(java.io.Serializable)
	 */
	@Override
	public Student findOne(Long primaryKey) throws SampleAppException {
		Student result = getRepository().findOne(primaryKey);
		Hibernate.initialize(result.getCourses());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.tp8.sampleapp.service.BaseEntityService#findByExample(org.springframework
	 * .data.domain.Pageable, org.tp8.sampleapp.model.AbstractModel)
	 */
	@Override
	public Page<Student> findByExample(Pageable pageable, Student example)
			throws SampleAppException {
		Page<Student> students = getRepository().queryByExample(pageable,
				example);
		for (Student student : students) {
			Hibernate.initialize(student.getCourses());
		}
		return students;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.tp8.sampleapp.service.intf.StudentService#findAll(org.springframework
	 * .data.domain.Pageable)
	 */
	@Override
	public Page<Student> findAll(Pageable pageable) throws SampleAppException {
		Page<Student> result = getRepository().findAll(pageable);
		for (Student student : result) {
			Hibernate.initialize(student.getCourses());
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.tp8.sampleapp.service.BaseEntityService#delete(java.io.Serializable)
	 */
	@Override
	public void delete(Long primaryKey) throws SampleAppException {
		getRepository().delete(primaryKey);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.tp8.sampleapp.service.intf.StudentService#validate(org.tp8.sampleapp
	 * .model.Student)
	 */
	@Override
	public RuleModelWrapper validate(Student t) throws SampleAppException {
		return ruleService.execute("kbStudent", t);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.tp8.sampleapp.service.intf.StudentService#count()
	 */
	@Override
	public long count() throws SampleAppException {
		return getRepository().count();
	}

}
